﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gnosis.Alexandria.Model.Commands
{
    public class InsertTextBuilder
        : CommandBuilderBase
    {
        public InsertTextBuilder(IIdentifiable record)
            : base(record)
        {
            var changes = record.Changes();
            if (changes.Count > 0)
            {
                var columns = new StringBuilder();
                var values = new StringBuilder();

                var delimiter = string.Empty;
                foreach (KeyValuePair<string, object> change in changes)
                {
                    var parameterName = GetParameterName(change.Key);
                    columns.AppendFormat("{0}{1}", delimiter, change.Key);
                    values.AppendFormat("{0}{1}", delimiter, parameterName);

                    AddParameter(parameterName, change.Value);
                    delimiter = ",";
                }

                AppendFormat("INSERT INTO {0} ({1}) VALUES ({2})", record.RecordName, columns, values);
            }
        }
    }
}
